package cn.ynrcb.personnelfile.sysadmin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.ynrcb.personnelfile.common.utils.PageUtils;
import cn.ynrcb.personnelfile.common.utils.Query;
import cn.ynrcb.personnelfile.sysadmin.dao.SysDictDao;
import cn.ynrcb.personnelfile.sysadmin.entity.SysDictEntity;
import cn.ynrcb.personnelfile.sysadmin.service.SysDictService;
import cn.ynrcb.personnelfile.sysadmin.vo.AddressDictVo;
import cn.ynrcb.personnelfile.sysadmin.vo.DictVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("sysDictService")
public class SysDictServiceImpl extends ServiceImpl<SysDictDao, SysDictEntity> implements SysDictService {

    @Autowired
    private CurrentUserServiceImpl currentUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SysDictEntity> queryWrapper = new QueryWrapper<SysDictEntity>();
        if(ObjectUtil.isNotNull(params.get("key"))){
            String key = Convert.toStr(params.get("key"));
            queryWrapper.like("label", key).or().like("module", key).or().like("type", key).or().like("value", key);
        }
        IPage<SysDictEntity> page = this.page(
                new Query<SysDictEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Map<String,List<DictVo>> getDataList(Map<String, Object> params) {
        List<DictVo> dataList = baseMapper.selectVoList(params);
        if("nomal".equals(Convert.toStr(params.get("module")))){
            //如果参数中包含了nomal，则加入角色数据字典
            List<DictVo> roleDictList = baseMapper.selectRoleVoList();
            dataList.addAll(roleDictList);
        }
        Map<String,List<DictVo>> dataMap = dataList.stream().collect(Collectors.groupingBy(p -> p.getType(),Collectors.toList()));
        return dataMap;
    }

    @Override
    public List<AddressDictVo> getAddressDataList(List<AddressDictVo> addressDictList,String id) {
        addressDictList = baseMapper.getAddressDataListByPid(id);
        if(CollectionUtil.isNotEmpty(addressDictList) && !"3".equals(addressDictList.get(0).getLevel())){
            //未到叶节点
            for (AddressDictVo vo : addressDictList) {
                vo.setChildren(getAddressDataList(addressDictList,vo.getId()));
            }
        }
        return addressDictList;
    }

    @Override
    public List<AddressDictVo> getAddressDataListById(List<AddressDictVo> addressDictList, String id) {
        addressDictList = baseMapper.getAddressDataListById(id);
        if(CollectionUtil.isNotEmpty(addressDictList) && !"3".equals(addressDictList.get(0).getLevel())){
            //未到叶节点
            for (AddressDictVo vo : addressDictList) {
                vo.setChildren(getAddressDataList(addressDictList,vo.getId()));
            }
        }
        return addressDictList;
    }

    @Override
    public List<AddressDictVo> getAddressList() {
        return baseMapper.getAddressList();
    }

    @Override
    public List<SysDictEntity> getPlatform() {
        return baseMapper.getPlatform();
    }

    @Override
    public List<SysDictEntity> getField() {
        return baseMapper.getField();
    }

    @Override
    public String getFieldByValue(Integer field) {
        return baseMapper.getFieldByValue(field);
    }

    @Override
    public String getSubFieldByValue(Integer field) {
        return baseMapper.getSubFieldByValue(field);
    }
}
